﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KP_CommonLibrary.KP_Business;
using KEEPER_BRR_13dotNET.DataAccess;
using System.Data;
using KP_CommonLibrary.KP_ObjectReferences;
using KP_CommonLibrary;

namespace KEEPER_BRR_13dotNET.BusinessLogic
{
    public class BL_KPHoSo : KP_BLListBase
    {
        DAL_KPHoSo DALHoSo;
        public BL_KPHoSo()
            : base()
        {
            DALHoSo = new DAL_KPHoSo();
            DataAccess = DALHoSo;
        }

        public void Insert(DataRow hosoRow, DataTable tblThanhVien, DataTable tblNganhNghe, DataTable tblGiayTo)
        {
            try
            {
                if (hosoRow.IsNull("Stt_Rec") || hosoRow["Stt_Rec"].ToString() == BL_KPCommon.EmptySttRec)
                    hosoRow["Stt_Rec"] = BL_KPCommon.GetNextSttRec();

                if (hosoRow.IsNull("Ma_Hs") || hosoRow["Ma_Hs"].ToString() == "0")
                    hosoRow["Ma_Hs"] = BL_KPCommon.GetNextHoSoSttRec();

                DAL_KPThanhVienHs dalThanhVien = new DAL_KPThanhVienHs();
                DAL_KPNganhHS dalNganhHs = new DAL_KPNganhHS();
                DAL_KPTPHoSo dalTpHoSo = new DAL_KPTPHoSo();

                DALHoSo.OpenConnection();
                DALHoSo.BeginTransaction();

                DataTable tblNewBn = BL_KPCommon.NextMa_Bn(DALHoSo.Transaction, hosoRow["Ma_LoaiHs"].ToString(), hosoRow["Ma_Hs"].ToString());


                hosoRow["So_Bn"] = tblNewBn.Rows[0][0];

                // hosoRow["So_Bn"] = BL_KPCommon.NextMa_Bn(DALHoSo.Transaction);
                DALHoSo.Insert(DALHoSo.Transaction, hosoRow);

                foreach (DataRow tvRow in tblThanhVien.Rows)
                {
                    tvRow["Stt_Rec"] = hosoRow["Stt_Rec"];
                    dalThanhVien.Insert(DALHoSo.Transaction, tvRow);
                }
                foreach (DataRow nnRow in tblNganhNghe.Rows)
                {
                    nnRow["Stt_Rec"] = hosoRow["Stt_Rec"];
                    dalNganhHs.Insert(DALHoSo.Transaction, nnRow);
                }
                foreach (DataRow gtRow in tblGiayTo.Rows)
                {
                    gtRow["Stt_Rec"] = hosoRow["Stt_Rec"];
                    dalTpHoSo.Insert(DALHoSo.Transaction, gtRow);
                }
                DALHoSo.Transaction.Commit();
                DALHoSo.Transaction = null;

            }
            catch (Exception ex)
            {
                if (DALHoSo.Transaction != null)
                {
                    DALHoSo.Transaction.Rollback();
                    DALHoSo.Transaction = null;
                }
                throw ex;

            }
            finally
            {
                DALHoSo.CloseConnection();
            }
        }


        public void Update(DataRow hosoRow, DataTable tblThanhVien, DataTable tblNganhNghe, DataTable tblGiayTo)
        {
            try
            {
                //if (hosoRow.IsNull("Stt_Rec") || hosoRow["Stt_Rec"].ToString() == "000000")
                //    hosoRow["Stt_Rec"] = BL_KPCommon.GetNextSttRec();

                //if (hosoRow.IsNull("Ma_Hs"))
                //    hosoRow["Ma_Hs"] = BL_KPCommon.GetNextHoSoSttRec();

                DAL_KPThanhVienHs dalThanhVien = new DAL_KPThanhVienHs();
                DAL_KPNganhHS dalNganhHs = new DAL_KPNganhHS();
                DAL_KPTPHoSo dalTpHoSo = new DAL_KPTPHoSo();

                DALHoSo.OpenConnection();
                DALHoSo.BeginTransaction();
                DALHoSo.XoaTruocKhiCapNhat(DALHoSo.Transaction, hosoRow["Stt_Rec"].ToString());
                // hosoRow["So_Bn"] = BL_KPCommon.NextMa_Bn(DALHoSo.Transaction);
                DALHoSo.Insert(DALHoSo.Transaction, hosoRow);

                foreach (DataRow tvRow in tblThanhVien.Rows)
                {
                    tvRow["Stt_Rec"] = hosoRow["Stt_Rec"];
                    dalThanhVien.Insert(DALHoSo.Transaction, hosoRow);
                }
                foreach (DataRow nnRow in tblNganhNghe.Rows)
                {
                    nnRow["Stt_Rec"] = hosoRow["Stt_Rec"];
                    dalNganhHs.Insert(DALHoSo.Transaction, nnRow);
                }
                foreach (DataRow gtRow in tblGiayTo.Rows)
                {
                    gtRow["Stt_Rec"] = hosoRow["Stt_Rec"];
                    dalTpHoSo.Insert(DALHoSo.Transaction, gtRow);
                }
                DALHoSo.Transaction.Commit();
                DALHoSo.Transaction = null;

            }
            catch (Exception ex)
            {
                if (DALHoSo.Transaction != null)
                {
                    DALHoSo.Transaction.Rollback();
                    DALHoSo.Transaction = null;
                }
                throw ex;

            }
            finally
            {
                DALHoSo.CloseConnection();
            }
        }

        public DataRow SelectBySttRec(string sttRec)
        {
            return DALHoSo.SelectBySttRec(sttRec);
        }
        public DataTable SelectNganhHSBySttRec(string sttRec)
        {
            DAL_KPDmNganh dlNganh = new DAL_KPDmNganh();
            return dlNganh.SelectByHoSoSttRec(sttRec);
        }
        public DataTable SelectThanhVienBySttRec(string sttRec)
        {
            DAL_KPThanhVienHs dlTv = new DAL_KPThanhVienHs();
            return dlTv.SelectBySttRec(sttRec);
        }
        public DataTable SelectTPHoSoBySttRec(string sttRec)
        {
            DAL_KPTPHoSo dlTpHs = new DAL_KPTPHoSo();
            return dlTpHs.SelectBySttRec(sttRec);
        }
        public DataTable SelectByMaTtAndMaVt(string maTt, string maVt)
        {
            DAL_KPTPHoSo dlTpHs = new DAL_KPTPHoSo();
          return  DALHoSo.SelectByMaTtAndMaVt(maTt, maVt);
        }

        public DataTable FindDangKyKdByKeyword(string keyword, int trangthai1, int trangthai2, string maVtri)
        {
            return DALHoSo.FindDangKyKdByKeyword(keyword, trangthai1, trangthai2, maVtri);
        }

        public void DuyetHoSo(string sttRec, int ma_tt, int nextMa_tt, string lyDo, int type)
        {
            DALHoSo.DuyetHoSo(sttRec, ma_tt, nextMa_tt, lyDo, KP_OUser.User_Name, KP_SYS_VARIABLE.M_CLIENT0, type);
        }

        public void KetThucDuyetHs(string sttRec, int ma_tt, int nextMa_tt, string lyDo)
        {
            DALHoSo.KetThucDuyetHs(sttRec, ma_tt, nextMa_tt, lyDo, KP_OUser.User_Name, KP_SYS_VARIABLE.M_CLIENT0);
        }
        public void TraHoSo(string sttRec)
        {
            DALHoSo.TraHoSo(sttRec, KP_OUser.User_Name, KP_SYS_VARIABLE.M_CLIENT0);
        }

        internal bool KiemTraTruocKhiCapLai(string soGp, out string message)
        {
            throw new NotImplementedException();
        }
    }
}
